﻿using System;
using System.Text;

using TempEmployees.DAL;
using TempEmployees.BLL.Database;

namespace TempEmployees.Web
{
    public partial class SystemData : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonAddSemester_Click(object sender, EventArgs e)
        {
            string name = TextboxSemesterName.Text;
            DateTime start, end;
            DateTime.TryParse(TextboxStartDate.Text, out start);
            DateTime.TryParse(TextboxEndDate.Text, out end);

            StringBuilder builder = new StringBuilder("Input error: ");
            if (name.Length == 0) builder.Append(" 'Name' ");
            if (start == default(DateTime)) builder.Append(" 'Start Date' ");
            if (end == default(DateTime)) builder.Append(" 'End Date' ");
            if (builder.ToString() != "Input error: ")
            {
                LabelSemesterMessage.Text = builder.ToString();
                return;
            }

            Semester semester = new Semester(name, start, end);
            if (!DbQuery.Insert<Semester>(semester))
            {
                LabelSemesterMessage.Text = "Failed to add semester";
            }
            else
            {
                LabelSemesterMessage.Text = "Semester added";
                TextboxSemesterName.Text = string.Empty;
                TextboxStartDate.Text = string.Empty;
                TextboxEndDate.Text = string.Empty;
                SemestersGridView.DataBind();
            }
        }

        protected void ButtonAddDep_Click(object sender, EventArgs e)
        {
            string name = TextBoxDepName.Text;
            if (name == string.Empty)
            {
                LabelDepMessage.Text = "Input error: Name";
                return;
            }

            Department d = new Department(name);
            if (!DbQuery.Insert<Department>(d))
            {
                LabelDepMessage.Text = "Failed to add department";
            }
            else
            {
                LabelDepMessage.Text = "Department added";
                TextBoxDepName.Text = string.Empty;
                DepartmentsGridView.DataBind();
            }
        }
    }
}